package org.gl.customview.listview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
/**
 * the demo for ListViewEmpty
 * @author Administrator
 *
 */
public class EmptyListViewActivity extends Activity {

	private static ArrayList<String> EMPTY = new ArrayList<String>();
	private ArrayList<String> CHEESES = new ArrayList<String>();
	private CheeseAdapter mAdapter;
	private ListView mListView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ListViewEmpty mListViewEmpty = new ListViewEmpty(this);
		mListViewEmpty.setLayoutParams(new LayoutParams(480, 600));
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(1);
		ll.addView(mListViewEmpty);
		Button b = new Button(this);
		ll.addView(b);
		setContentView(ll);
		for (int i = 0; i < 10; i++) {
			CHEESES.add(i + "");
		}
		mListView = mListViewEmpty;
		mAdapter = new CheeseAdapter(CHEESES);
		mListView.setAdapter(mAdapter);
		b.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				if (mAdapter.isEmpty()) {
					onSetData(null);
				} else {
					onSetEmpty(null);
				}

			}
		});
	}

	public void onSetEmpty(View v) {
		mAdapter.changeData(EMPTY);
	}

	public void onSetData(View v) {
		mAdapter.changeData(CHEESES);
	}

	private class CheeseAdapter extends BaseAdapter {

		private ArrayList<String> mData;

		public CheeseAdapter(ArrayList<String> data) {
			mData = data;
		}

		public void changeData(ArrayList<String> data) {
			mData = data;
			notifyDataSetChanged();
		}

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public String getItem(int position) {
			return mData.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = getLayoutInflater().inflate(android.R.layout.simple_list_item_1,
						parent, false);
			}

			((TextView) convertView).setText(getItem(position));

			return convertView;
		}
	}
}
